<!-- Imports -->
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Hashtable"%>
<%@page import="canss.supports.basic"%>
<%@page import="canss.supports.Makequery_select"%>
<!-- Initiation -->
<%
response.setContentType("text/html; charset=UTF-8");
canss.web.controls.WebDBControl webdbcontrol = null;
webdbcontrol = (webdbcontrol==null)?new canss.web.controls.WebDBControlBean():webdbcontrol;
%>
<!-- Preparing inputs -->
<%
//_pagenum = request.getParameter("_pagenum");
String _admin = basic.IIf((String)session.getAttribute("_admin"), "user");
String _rurl = basic.IIf((String)session.getAttribute("_rurl"), "");
int _startfrom = Integer.valueOf(basic.IIf(request.getParameter("_startfrom"), "0"));
int _howmany = Integer.valueOf(basic.IIf(request.getParameter("_howmany"), "1"));
String _viewtype = basic.IIf(request.getParameter("_viewtype"), "iconic"); //table(cur), iconic(tistory), text(no thumb)
String _orderby = basic.IIf(request.getParameter("_orderby"), "");
String _orderbytype = basic.IIf(request.getParameter("_orderbytype"), "");
String _orderbytype_rev="";
if(basic.isNotEmpty(_orderby))
{
	if(basic.isEmpty(_orderbytype) || _orderbytype.equalsIgnoreCase(" asc"))
		_orderbytype_rev = " desc";
	else if(_orderbytype.equalsIgnoreCase(" desc"))
		_orderbytype_rev = " asc";
}
String _debug = basic.IIf(request.getParameter("_debug"), "");
String _func = basic.IIf(request.getParameter("_func"), "");
String _search_opt = basic.IIf(request.getParameter("_search_opt"), "");
String _search_word = basic.IIf(request.getParameter("_search_word"), "");
String _condition = basic.IIf(request.getParameter("_condition"), "");
int curpage = (_startfrom/_howmany)+1;
int maximum=0;
int maxpage=0/* =(maximum/howmany) */;
int i=0;
%>
<!-- Query Compilation -->
<%
	Makequery_select query = new Makequery_select()
.Select("R.maximum as maximum, tblContentDescription.tblCID_cid as cid, dc_description as description, dc_title as title, xmpdm_genre as genre, xmp_rating as agelimit, xmpdm_duration as duration,  cansscontent_thumbnail as thumbnail, location as src, xmpdm_moddate as uploadedtime,  dc_publisher as publisher, canssContentDescription_owner as owner, cansscontentdescription_status as status, oneLineDesc")
.PushFrom("tblContentDescription left outer join tblContent on tblContent.tblCID_cid=tblContentDescription.tblCID_cid join tblEventDescription on cansscontentdescription_status=eventid JOIN tblLocation ON tblContentDescription.tblCID_cid=tblLocation.tblContent_tblCID_cid")
.OrderBy(_orderby+_orderbytype)
.Limit(String.format("%s, %s", _startfrom, _howmany));

Makequery_select subquery = new Makequery_select()
.Select("count(*) as maximum")
.PushFrom("tblContentDescription LEFT OUTER JOIN tblContent ON tblContent.tblCID_cid = tblContentDescription.tblCID_cid JOIN tblEventDescription ON cansscontentdescription_status = eventid JOIN tblLocation ON tblContentDescription.tblCID_cid = tblLocation.tblContent_tblCID_cid ")
.PushWhere(basic.IIf(_search_word, String.format("%s like '%%%s%%'", query.NameAlias2Origin(_search_opt), _search_word), ""))
.PushWhere(basic.IIf(_condition, String.format("%s", _condition), ""));

query
.PushWhere(basic.IIf(_search_word, String.format("%s like '%%%s%%'", query.NameAlias2Origin(_search_opt), _search_word), ""))
.PushWhere(basic.IIf(_condition, String.format("%s", _condition), ""))
.PushFrom(subquery.toString(true, "R"));/// from 이 pushfrom으로 바뀌어야함.

if(basic.isNotEmpty(_debug))//debug msg
	out.write(query.toString());

Iterator<String[]> iter = null;
Hashtable<String, Integer> hash = null;
try
{
	LinkedList<String[]> res = webdbcontrol.Select(query);
	iter = res.iterator();
	if(iter.hasNext())
	{
		String[] curhashtmp = iter.next();
		hash = new Hashtable<String, Integer>(curhashtmp.length);
		for(i=0; i<curhashtmp.length; i++)
	hash.put(curhashtmp[i], i);
	}
	String[] last = res.getLast();
	if(last!=null && last!=res.getFirst())
		maximum = Integer.valueOf(basic.IIf(last[hash.get("maximum")], "0"));
	else
		maximum = 0;
}catch(Exception e)
{
	e.printStackTrace();
}
%>


<!-- Scripts Start -->
<script type="text/javascript">
/*
	doesn't work...
	var thumbimgfullsize=0;
	$("img.thumbnail").hover(
	function(){
		$("#img_hover").css("display", "block");
		$("#img_hover").find("img:first").attr("src", $(this).attr("src"));
	},
	function(){
		$("#img_hover").css("display", "none");
	})
	.mousemove(function()
	{
		xx=document.body.scrollLeft+event.x;
		yy=document.body.scrollTop+event.y;
		scr_height= (screen.availHeight - self.innerHeight);
		
		if(thumbimgfullsize==0)
			thumbimgfullsize = $("#img_hover > img:first").css("height").replace("px","");
		
		$("#img_hover").css("left", xx+10)
		$("#img_hover").css("top", yy-(thumbimgfullsize+10));
		//$("#img_hover").css("top", (yy+10-thumbimgfullsize)>0?(yy+10-thumbimgfullsize):0);
	});
*/
	<%@ include file="lib/listcontents.js" %>
</script>
<!-- Scripts End -->

<!-- Data Start -->
<div id="div_contentsdesc">

	<div id="img_hover" style="display:none;position:absolute;">
		<img src="" style="padding: 0px; margin: 0px;"/>
	</div>
 
	<ul class="listr">
		<li>
			<select id="sel_howmany">
				<option value="5">5 items per page</option>
				<option value="10">10 items per page</option>
				<option value="20">20 items per page</option>
				<option value="30">30 items per page</option>
				<option value="50">50 items per page</option>
				<option value="100">100 items per page</option>
			</select>
		</li>
		<li>
			<input type="button" value="Refresh" onclick="<%=String.format("%s('%s','%s','%s','%s','%s','%s','%s');", _func, _startfrom, _howmany, _orderby, _orderbytype, _search_opt, _search_word, _viewtype) %>" />
		</li>
		<li class="rightalign">
			<a href="#" onclick="<%=String.format("%s('%s','%s','%s','%s','%s','%s','%s');", _func, _startfrom, _howmany, _orderby, _orderbytype, _search_opt, _search_word, "table") %>return false;" <%="iconic".equalsIgnoreCase(_viewtype)?"class=\"filter-50\"":"" %>><img src="design/img/list-board.gif"/>Board</a>
		</li>
		<li class="rightalign">
			<a href="#" onclick="<%=String.format("%s('%s','%s','%s','%s','%s','%s','%s');", _func, _startfrom, _howmany, _orderby, _orderbytype, _search_opt, _search_word, "iconic") %>return false;" <%="table".equalsIgnoreCase(_viewtype)?"class=\"filter-50\"":"" %>><img src="design/img/list-album.gif"/>Iconic</a>
		</li>
	</ul>
	
	<%
	if(_viewtype.equalsIgnoreCase("table"))
	{
	%>
		<!-- Table Representation Start -->
		<%
		out.write("<table class=\"datatable\" id=\"tbl_contentsdesc\" >");
		out.write("<tbody>");
		try
		{
			//col names.
			out.write("<tr>");
			out.write("  <th>No.</th>");
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Title</a></th>", _func, _startfrom, _howmany, "title", ("title".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Thumbnail</a></th>", _func, _startfrom, _howmany, "thumbnail", ("thumbnail".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Play Button</a></th>", _func, _startfrom, _howmany, "src", ("src".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Uploaded Time</a></th>", _func, _startfrom, _howmany, "uploadedtime", ("uploadedtime".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Owner</a></th>", _func, _startfrom, _howmany, "owner", ("owner".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write(String.format("  <th><a onclick=\"%s('%s','%s','%s','%s','%s','%s','%s');\">Content Status</a></th>", _func, _startfrom, _howmany, "status", ("status".equals(_orderby))?_orderbytype_rev:"", _search_opt, _search_word, _viewtype));
			out.write("  <th>Content Publish</th>");
			out.write("</tr>");
			//data
			i=(_startfrom+1);
			while(iter!=null && iter.hasNext())
			{
				String[] cur = iter.next();
				
				out.write("<tr>");
				
				// numbers
				out.write(String.format(
						"<td>%s%s</td>", 
						i++, 
						"<input id=\"hid_maximum\" name=\"hid_maximum\" type=\"hidden\" value=\""+cur[hash.get("maximum")]+"\" />"));
				//table-body
				out.write(String.format("<td style=\"width:200px\" class=\"text\">%s</td>", cur[hash.get("title")]));
				out.write(basic.IIf(cur[hash.get("thumbnail")],
						String.format("<td><img class=\"thumbnail\" src=\"%s\"/></td>", cur[hash.get("thumbnail")]),
						String.format("<td><div class=\"emptythumbnail\"></div></td>")));
				out.write(basic.IIf(cur[hash.get("src")],
						String.format(
								"<td>"+
								"  <a onclick=\"window.open('testplayer/content_testview.jsp?_src=%s&_image=%s', 'pop_view_player', 'width=380,height=360');\">"+
								"    <img src=\"design/img/t_play.png\" class=\"playbutton\"/>"+
								"  </a>"+
								"</td>", 
								cur[hash.get("src")], cur[hash.get("thumbnail")]),
						String.format("<td>-</td>")));
				out.write(String.format("<td>%s</td>", cur[hash.get("uploadedtime")]));
				out.write(String.format("<td>%s</td>", cur[hash.get("owner")]));
				out.write(String.format("<td>%s</td>", cur[hash.get("oneLineDesc")]));
				out.write(String.format("<td><div id=\"div_%s\" class=\"pubcommand\" style=\"display:inline;\"><form action=\"/CANSSWebService/svtAjaxContentStatusUpdate\" method=\"post\"><input type=\"hidden\" name=\"_cid\" value=\"%s\"/><input type=\"hidden\" name=\"_ajax\" value=\"%s\"/><input type=\"hidden\" name=\"_status\" value=\"%s\"/><input type=\"hidden\" name=\"_rurl\" value=\"%s\"/><input type=\"hidden\" name=\"_donotupdate\" value=\"1\"/></form></div></td>", cur[hash.get("cid")], cur[hash.get("cid")], _admin, cur[hash.get("status")], _rurl));
				
				out.write("</tr>");
			}
			if(i==(_startfrom+1))
			{//null table
				out.write("<tr>");
				out.write(" <td>-</td>");
				out.write(" <td colspan=\"7\">No item matched</td>");
				out.write("</tr>");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		out.write("</tbody>");
		out.write("</table>");
		%>
		<!-- Table Representation End -->
	<%
	}else if(_viewtype.equalsIgnoreCase("iconic"))
	{
	%>
		<!-- Iconic Representation Start -->
		<%
		out.write("<div id=\"div_contentsdescinner\">");
		out.write(" <div id=\"div_contentsdesc_detail\" >");
		out.write(" </div>");
		
		out.write(" <ul class=\"contentsdesc_list\" >");
		try
		{
			while(iter!=null && iter.hasNext())
			{
				String[] cur = iter.next();
				
				out.write("<li>");
				out.write(" <dl>");
				out.write("  <dd>");
				out.write("   <p>");
				//image
				out.write("		 <table><tr><td style=\"margin:0px;padding:0px;border:0px;height:98px;\">");
				if(basic.isNotEmpty(cur[hash.get("thumbnail")]))
					out.write(String.format("<img width=\"130\" height=\"98\" alt\"test\" src=\"%s\" style=\" position:relative; display:inline; top:0px;\" />", cur[hash.get("thumbnail")]));
				else
					out.write(String.format("<img width=\"130\" height=\"98\" alt\"test\" src=\"%s\" style=\" position:relative; display:inline; top:0px; border:1px dashed;\" />", "design/img/blank.gif"));					
				out.write("			</td><td style=\"margin:0px;padding:0px;border:0px;width:0px;\">");
				out.write(String.format("<a onclick=\" see_contentsdesc_detail('%s', '#div_contentsdesc_detail'); \">", cur[hash.get("cid")]));////////////////
				if(basic.isNotEmpty(cur[hash.get("thumbnail")]))
					out.write("			 <div class=\"playbutton2\" style=\"	left:-132px;\" />");
				else
					out.write("			 <div class=\"playbutton2disabled\" style=\"	left:-132px;\" />");
				out.write("       </a>");
				out.write("			</td></tr></table>");
				//title
				out.write(String.format("      <strong>%s</strong>", cur[hash.get("title")]));
				if(hash.get("publisher")!=null && basic.isNotEmpty(cur[hash.get("publisher")]))
					out.write(String.format("      <span>Uploaded by '%s(%s)'</span>", cur[hash.get("owner")], cur[hash.get("publisher")]));
				else
					out.write(String.format("      <span>Uploaded by '%s'</span>", cur[hash.get("owner")]));
				out.write(String.format("      <span>'%s'%s</span>", cur[hash.get("oneLineDesc")], basic.padding(20, "&nbsp;")));
				out.write("   </p>");
				out.write(String.format("      <div id=\"div_%s\" style=\"border:1px dashed; \" class=\"pubcommand\"><form style=\"\" action=\"/CANSSWebService/svtAjaxContentStatusUpdate\" method=\"post\"><input type=\"hidden\" name=\"_cid\" value=\"%s\"/><input type=\"hidden\" name=\"_ajax\" value=\"%s\"/><input type=\"hidden\" name=\"_status\" value=\"%s\"/><input type=\"hidden\" name=\"_rurl\" value=\"%s\"/><input type=\"hidden\" name=\"_donotupdate\" value=\"1\"/></form></div>", cur[hash.get("cid")], cur[hash.get("cid")], _admin, cur[hash.get("status")], _rurl));
				out.write("  </dd>");
				out.write(" </dl>");
				out.write("</li>");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		out.write(" </ul>");
		out.write("</div>");
		%>
		<!-- Iconic Representation End -->
	<%
	}else if(_viewtype.equalsIgnoreCase("text"))
	{
	%>
		<!-- Text Representation Start -->
		<%
		%>
		<!-- Text Representation End -->
	<%
	}
	%>	
	<%//_func
		maxpage = ((maximum-1)/_howmany)+1;
		out.write("<center>");
		out.write("| ");
		for(i=0; i<maxpage; i++)
		{
			if( (i+1)!=curpage )
				out.write(String.format("<a class=\"page\" onclick=\"%s('%s','%s','%s','%s','%s','%s','%s')\">%s</a> | ", _func, (i*_howmany), (_howmany), _orderby, _orderbytype, _search_opt, _search_word, _viewtype, (i+1)));
			else
				out.write(String.format("<a class=\"pagesel\" onclick=\"%s('%s','%s','%s','%s','%s','%s','%s')\">%s</a> | ", _func, (i*_howmany), (_howmany), _orderby, _orderbytype, _search_opt, _search_word, _viewtype, (i+1)));
		}
		out.write("</center>");
	%>
	<div id="div_search" style="text-align:center">
		<select id="sel_search_opt">
			<option value="title">Title</option>
			<option value="owner">Owner</option>
			<option value="oneLineDesc">Content Status</option>
		</select>
		<input class="textinput" type="text" id="input_search_word" />
		<input type="button" id="bt_search" value="Search" />
	</div>
	<!-- Data End -->
	
	<!-- Logging -->
	<%
		//logging
		{
			canss.webservice.service.PortalIntraWebServiceControl portalctl = null;
			portalctl = (portalctl==null)?new canss.webservice.service.PortalIntraWebServiceControlBean():portalctl;
			portalctl.addNewEvent(
					basic.PORTAL_MODULE_ID, 
					basic.LOG_PORTAL_CONTENTSEARCH_REQUEST, 
					canss.encoder.DBStringEncoder.getServerTime().getTime(), 
					String.format(
							"<log><pid>%s</pid><name>%s</name><sessionkey>%s</sessionkey><type>%s</type><search_word>%s</search_word></log>", 
							basic.IIf((String)session.getAttribute("_pid"), ""), 
							basic.IIf((String)session.getAttribute("_name"), ""), 
							basic.IIf((String)session.getAttribute("_sessionkey"), ""), 
							basic.IIf((String)session.getAttribute("_loginedas"), ""), 
							_search_word
							));
		}
	%>
</div>